package org.zcl.mongo.model;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * .
 *
 * @author zhuchenglin
 * @since 2024/8/5 20:18
 */
@Data
public class QueryExpressionInfo {

    private String fieldName;

    private String operator;

    private String fieldValue;

    public static QueryExpressionInfo from(String expression) {
        QueryExpressionInfo expressionInfo = new QueryExpressionInfo();
        String[] words = StringUtils.split(expression, " ", 3);
        String fieldName = words[0];
        String operator = words[1];
        String fieldValue = rebuildFieldValue(words[2]);

        expressionInfo.setFieldName(fieldName);
        expressionInfo.setOperator(operator);
        expressionInfo.setFieldValue(fieldValue);
        return expressionInfo;
    }

    public static String rebuildFieldValue(String fieldValue) {
        return fieldValue.replace("$L", "(").replace("$R", ")").replace("$A", "AND").replace("$O", "OR").replace("$S", " ");
    }
}
